<template>
  <Container class="full-sub-gift-sale">
    <div slot="header" class="container-header title title-center">满减送促销
      <Button size="large" class="fl btn-back" icon="ios-arrow-back" @click="jumpPage">返回</Button>
    </div>
    <!-- 基础设置 -->
    <DefaultEditItem ref="fullSubForm" :form="$store.state.fullSubGiftSale.form" />
    <div slot="footer" class="container-footer bg-white">
      <Button type="primary" size="large" class="ml" @click="onClickNext">下一步</Button>
    </div>
  </Container>
</template>
<script>
import DefaultEditItem from './edit/DefaultEditItem'
export default {
  components: {
    DefaultEditItem
  },
  data () {
    return {
      id: this.$route.query.id || '',
      type: this.$route.query.type || '',
      isEmpty: false, // 是否清空数据
      isSave: true // 是否保存数据
    }
  },
  mounted () {
    // this.id && this.getDetailData()
  },
  methods: {
    getDetailData () {
      this.$ajaxGet('/activity/full_cut_gift/detail/' + `${this.id}`).then(data => {
        this.$store.state.fullSubGiftSale.form.remarks = data.activityInfoDTO.activityDescription//活动备注
        this.$store.state.fullSubGiftSale.form.name = data.activityInfoDTO.activityName //促销名称
        this.$store.state.fullSubGiftSale.form.grade = this.promotionMemberScope = data.activityInfoDTO.storeTypeEnumList //活动对象
        this.$store.state.fullSubGiftSale.form.date = [new Date(data.activityInfoDTO.startDate), new Date(data.activityInfoDTO.endDate)]
        this.$store.state.fullSubGiftSale.form.con = data.activityFullCutGiftDTO.discountCondition //优惠条件
        this.$store.state.fullSubGiftSale.form.reduce = data.activityFullCutGiftDTO.discountCutAmount //优惠内容
        this.$store.state.fullSubGiftSale.form.giftCheck = data.activityFullCutGiftDTO.isGiftProduct // 是否赠品
        this.$store.state.fullSubGiftSale.form.isReduceDisable = !data.activityFullCutGiftDTO.discountCutAmount
        this.$store.state.fullSubGiftSale.form.reduceCheck = !!data.activityFullCutGiftDTO.discountCutAmount
        this.$store.state.fullSubGiftSale.form.addCheck = !!data.activityFullCutGiftDTO.addBuyAmount
        this.$store.state.fullSubGiftSale.form.isAddDisable = !data.activityFullCutGiftDTO.addBuyAmount
        this.$store.state.fullSubGiftSale.form.add = data.activityFullCutGiftDTO.addBuyAmount // 加价购
        this.$store.state.fullSubGiftSale.form.isParcel = data.activityFullCutGiftDTO.isParcel // 包邮
      })
    },
    onClickNext () {
      let self = this
      this.$refs.fullSubForm.$refs.form.validate(function (vadate) {
        if (vadate) {
          if (self.$store.state.fullSubGiftSale.form.reduceCheck || self.$store.state.fullSubGiftSale.form.addCheck || self.$store.state.fullSubGiftSale.form.giftCheck) {
            let url = { name: 'sales/fullSubGift/select', query: {} }
            if (self.id) {
              url.query.id = self.id
              if (self.type) url.query.type = self.type
            }
            self.$router.push(url)
            self.$Message.success('保存成功')
          } else {
            self.$Message.success('优惠内容必须选择')
          }
        }
      })
    },
    jumpPage () {
      this.$router.back()
    }
  },
  beforeRouteEnter (to, from, next) {
    /*
        1. 路由从不是下一页进入的都清空
        2. 路由从下一页进入并且有id就不刷新接口--保存信息
      */
    next(vm => {
      if (from.name !== 'sales/fullSubGift/select') {
        vm.isEmpty = true
      } else {
        vm.isSave = false
      }
      if (vm.isEmpty) {
        if (vm.$store.state.fullSubGiftSale.form.giftCheck) {
          vm.$store.commit('setFullSubGiftSaleGiftEmpty')
        } else {
          vm.$store.commit('setFullSubGiftSaleEmpty')
        }
      }
      if (vm.id && vm.isSave) vm.getDetailData()
    })
  }
}
</script>

